import g_get from '@dfeidao/atom-web/global/get';
import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import get from '@dfeidao/atom-web/local/get';
import set from '@dfeidao/atom-web/local/set';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import error from '@dfeidao/atom-web/msgbox/error';
import success from '@dfeidao/atom-web/msgbox/success';
import render from '@dfeidao/atom-web/render/render';
import hide_loading from '../../atom/hide-loading';
import show_loading from '../../atom/show-loading';
import whoami from '../../atom/whoami';

import p004 from './p004';
import p006 from './p006';
interface IReply {
	comment_numbering: string;
	productid: string;
	reply_time: number;
	reply_time_str: string;
	dynamic_id: string;
	reply_content: string;
	respondent_numbering: string;
	reply_numbering: string;
	staff_name: string;
	headimg: string;
	statistical_magnitude: number;
	isa: number;
	auth_code: number;
}
function trim(str: string) {
	return str.replace(/(^\s*)|(\s*$)/g, "");
}  // 去空格
export default async function a011(fd: IFeidaoAiBrowserComponent, args: { currentTarget: HTMLElement }) {
	const page_name = fd.data.node.getAttribute('page-name');
	// 评论回复
	const comment_numbering = get(fd, 'comment_numbering');
	const reply_content = fd.data.node.querySelector<HTMLSelectElement>('[id="reply_' + comment_numbering + '"]').value;
	if (!trim(reply_content)) {
		return error('请输入发表内容');
	}
	const reply_time = new Date().getTime();
	const dynamic_id = get(fd, 'dynamic_id');
	const user = await whoami(fd);
	const config = user.params as { productid: string, staff_no: string; staff_name: string; deptid: string; };
	const productid = config.productid;
	const staff_no = config.staff_no;
	const staff_name = config.staff_name; const respondent_numbering = get(fd, 'staff_no');
	try {
		show_loading();
		const res = await nodejs(page_name + '/s006', {
			comment_numbering,
			productid,
			reply_time,
			dynamic_id,
			reply_content,
			respondent_numbering,
			staff_no,
			staff_name
		});
		hide_loading();
		if (res === 'success') {
			success('发表成功');
			fd.data.node.querySelector<HTMLSelectElement>('[id="reply_' + comment_numbering + '"]').value = '';
			let auth_code = '';
			if (page_name === 'depart-details') {
				auth_code = g_get('depart_details_auth_code');
			} else if (page_name === 'sign-details') {
				auth_code = g_get('sign_details_auth_code');
			} else if (page_name === 'grab-details') {
				auth_code = g_get('grab_details_auth_code');
			} else if (page_name === 'lottery-details') {
				auth_code = g_get('lottery_details_auth_code');
			}
			const query = {
				dynamic_id,
				comment_numbering,
				page_size: 5,
				page_no: 1,
				employee_no: respondent_numbering,
				auth_code,
				productid
			};
			set(fd, 'page_no_reply', query.page_no);
			const result = await nodejs<{ dataArray: IReply[], count: number, page_no: number, page_size: number }>(page_name + '/s005', query);
			render(fd, result.dataArray, p004, 'p000' + comment_numbering, 'inner');
			if (result.count <= query.page_size) {
				fd.data.node.querySelector<HTMLDivElement>('[comment_numbering="' + comment_numbering + '"').classList.add('display-n');
			} else {
				fd.data.node.querySelector<HTMLDivElement>('[comment_numbering="' + comment_numbering + '"').classList.remove('display-n');
			}
			for (let i = 0; i < result.dataArray.length; i++) {
				render(fd, result.dataArray[i], p006, 'p006' + result.dataArray[i].reply_numbering, 'inner');
			}
		} else {
			error('发表失败');
		}
	} catch (e) {
		hide_loading();
		error('发表失败');
	}
}
